package com.xd.clap.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xd.clap.entity.Advise;
import com.xd.clap.entity.Image;
import com.xd.clap.entity.Results;
import com.xd.clap.entity.request.ResultsParam;
import com.xd.clap.entity.response.Result;
import com.xd.clap.entity.response.ResultsResult;
import com.xd.clap.mapper.ResultMapper;
import com.xd.clap.services.AdviseService;
import com.xd.clap.services.ImageService;
import com.xd.clap.services.ResultsService;
import com.xd.clap.utils.SendMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ResultsServiceImpl implements ResultsService {

    @Resource
    private ResultMapper rm;
    @Resource
    private AdviseService as;
    @Resource
    private ImageService is;


    @Override
    public Result getResultsByAdviseId(Integer id) {
        Advise advise = as.getAdviseById(id);
        QueryWrapper<Results> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", id);
        queryWrapper.orderByAsc("create_time");
        List<Results> resultsList = rm.selectList(queryWrapper);
        if (resultsList.size() > 0) {
            for (Results result : resultsList) {
                List<Image> imageList = is.selectImageByRid(result.getId());
                result.setImageList(imageList);
            }
        }
        ResultsResult resultsResult = new ResultsResult();
        resultsResult.setAdvise(advise);
        resultsResult.setResultsList(resultsList);
        return Result.success(resultsResult, "查询成功");
    }

    @Override
    public Result uploadResults(ResultsParam resultsParam) {
        resultsParam.setCreateTime(new Date());
        int insert = rm.insert(resultsParam);
        Boolean isSet = as.setAdviseStateById(resultsParam.getAid(), 3);
        if (!isSet || insert <= 0) {
            return Result.fail(100, "处理失败");
        }
        int rid = resultsParam.getId();
        List<String> imgList = resultsParam.getImgList();
        for (String url : imgList) {
            Image image = new Image();
            image.setUrl(url);
            image.setRid(rid);
            Integer i = is.insertImage(image);
            if (i <= 0) {
                return Result.fail(100, "图片上传失败");
            }
        }
        return Result.success(null, "处理成功");
    }

    @Override
    public Result rejectedResults(ResultsParam resultsParam) {
        UpdateWrapper<Results> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", resultsParam.getId());
        updateWrapper.set("reason", resultsParam.getReason());
        int update = rm.update(null, updateWrapper);
        Boolean isSet = as.setAdviseStateById(resultsParam.getAid(), 2);
        if (isSet && update > 0) {
            String phone = resultsParam.getPhone();
            Boolean isSend = SendMsg.sendMsg(phone);
            return Result.success(isSend, "驳回成功");
        }
        return Result.fail(100, "驳回失败");
    }

    @Override
    public Result passResults(Integer aid) {
        Boolean isSet=as.setAdviseStateById(aid,4);
        if(isSet){
            return Result.success(null,"通过成功");
        }
        return Result.fail(100,"通过失败");
    }
}
